package com.wjp.banquet.common.filter;

import com.google.common.util.concurrent.RateLimiter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author 王君平
 * @Date 2023/11/20 13:34
 * @Description
 */
public class RateLimiterFilter implements Filter {

    private static RateLimiter limiter = RateLimiter.create(5);

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request =  (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        limiter.acquire();
        filterChain.doFilter(request, response);
        return;
    }
}
